using MicroMsg.Common.Utils;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Threading;
namespace MicroMsg.UI.UserContrl
{
	public static class RefreshTimer
	{
		private const string TAG = "RefreshTimer";
		private static DispatcherTimer Timer;
		private static Dictionary<DependencyObject, UIAction> ActionDic = new Dictionary<DependencyObject, UIAction>();
		private static int count = 0;
		private static bool isInit = false;
		private static void Init()
		{
			if (RefreshTimer.isInit)
			{
				return;
			}
			RefreshTimer.Timer = new DispatcherTimer();
			RefreshTimer.Timer.add_Tick(new EventHandler(RefreshTimer.Timer_Tick));
			RefreshTimer.Timer.set_Interval(new TimeSpan(0, 0, 0, 60));
			RefreshTimer.isInit = true;
		}
		private static void PushAction(UIAction action)
		{
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			if (phoneApplicationFrame == null)
			{
				return;
			}
			BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
			if (basePage == null)
			{
				return;
			}
			basePage.actionService.Push(action);
		}
		private static void Timer_Tick(object sender, EventArgs e)
		{
			Log.smethod_5("RefreshTimer", "Timer_Tick to refresh Time");
			using (Dictionary<DependencyObject, UIAction>.Enumerator enumerator = RefreshTimer.ActionDic.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<DependencyObject, UIAction> current = enumerator.get_Current();
					UIAction value = current.get_Value();
					if (value != null)
					{
						RefreshTimer.PushAction(value);
					}
				}
			}
		}
		public static void AddRefreshAction(DependencyObject uiobj, UIAction action)
		{
			if (uiobj == null)
			{
				return;
			}
			RefreshTimer.Init();
			if (!RefreshTimer.ActionDic.ContainsKey(uiobj))
			{
				if (RefreshTimer.count == 0)
				{
					RefreshTimer.Timer.Start();
					Log.smethod_5("RefreshTimer", " Timer.Start();");
				}
				RefreshTimer.count++;
			}
			RefreshTimer.ActionDic.set_Item(uiobj, action);
		}
		public static void RemoveRefreshAction(DependencyObject uiobj)
		{
			if (uiobj == null)
			{
				return;
			}
			if (!RefreshTimer.ActionDic.ContainsKey(uiobj))
			{
				return;
			}
			RefreshTimer.ActionDic.Remove(uiobj);
			if (RefreshTimer.count == 1)
			{
				RefreshTimer.Timer.Stop();
				Log.smethod_5("RefreshTimer", " Timer.stop();");
			}
			RefreshTimer.count--;
		}
	}
}
